<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Auth
 * @subpackage Adapter
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Digest.php 23775 2011-03-01 17:25:24Z ralph $
 */


/**
 * @see Zend_Auth_Adapter_Interface
 */
require_once 'Zend/Auth/Adapter/Interface.php';

/**
 * @category   Zend
 * @package    Zend_Auth
 * @subpackage Adapter
 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Ext_Auth_Adapter_Douban implements Zend_Auth_Adapter_Interface
{

    /**
     * Sets adapter options
     *
     * @param  mixed $username
     * @param  mixed $password
     * @return void
     */
    public function __construct()
    {
    }
    

    /**
     * Defined by Zend_Auth_Adapter_Interface
     *
     * @throws Zend_Auth_Adapter_Exception
     * @return Zend_Auth_Result
     */
    public function authenticate()
    {
    	$config = array(
		    'callbackUrl' 		=> 'http://qb.doudang.com/default/auth/oauth2/',
		    'siteUrl' 			=> 'http://www.douban.com/service/auth',
		    'consumerKey' 		=> '0bb6c3b69737434423244b23c5a5c9cc',
		    'consumerSecret' 	=> 'fcb5adcfa1243596'
		);
		$consumer = new Zend_Oauth_Consumer($config);
		
		if (!isset($_SESSION['ACCESS_TOKEN'])) {
			if (!empty($_GET) && isset($_SESSION['REQUEST_TOKEN'])) {
				$_SESSION['ACCESS_TOKEN'] = serialize($consumer->getAccessToken($_GET, unserialize($_SESSION['REQUEST_TOKEN'])));
				unset($_SESSION['REQUEST_TOKEN']);
			}
			else {
				$_SESSION['REQUEST_TOKEN'] = serialize($consumer->getRequestToken());
				$consumer->redirect();
			}
		}
		else 
		{
			$accessToken = unserialize($_SESSION['ACCESS_TOKEN']);

			$twitter = new Ext_Service_Douban(array(
			    'accessToken' 		=> $accessToken,
				'consumerKey' 		=> '0bb6c3b69737434423244b23c5a5c9cc',
	        	'consumerSecret' 	=> 'fcb5adcfa1243596'
			), $consumer);
			
			$response = $twitter->account->verifyCredentials();
		}
    }
    
    
}
